package com.itbaima.edu.info.manager.dao;

import com.itbaima.edu.info.manager.domain.Student;

/**
 * Created by byakuya on 2020/8/5
 */
public class StudentDao {  //库管

    //创建学生对象数组
    private static Student[] stus  = new Student[5];

    public boolean addStudent(Student stu) {

        //添加学生到学生数组
        int index = -1;  //假设数组已慢，没有null的元素
        for (int i = 0; i < stus.length; i++) {
            Student student = stus[i];
            if (student == null){
                index = i;  //找到null，记录索引值
                break;
            }
        }
        if (index == -1){  //装满了
            return false;
        }else {
            stus[index] = stu;
            return true;
        }
    }

    public Student[] findAllStudent() {
        return stus;
    }

    public void deleteStudentById(String delId) {
        //查找ID在容器中索引位置，将该索引位置使用null覆盖
        int index = getIndex(delId);
        //将该索引位置使用null覆盖
        stus[index] = null;
    }
    public int getIndex(String id) {  //获取ID在学生数组中的索引
        int index = -1;
        for (int i = 0; i < stus.length; i++) {
            Student student = stus[i];
            if (student != null && student.getId().equals(id)){
                index = i;
                break;
            }
        }
        return index;
    }

    public void updateStudent(String updateId, Student newStu) {
        //查找updateId，返回在容器中位置
        int index = getIndex(updateId);
        //修改
        stus[index] = newStu;
    }
}
